package com.h2cloud.controller;

import com.h2cloud.entity.BaseResp;
import com.h2cloud.entity.Result;
import com.h2cloud.service.ResultService;
import com.h2cloud.utils.JWTUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.List;

/**
 * (Result)表控制层
 *
 * @author makejava
 * @since 2020-10-11 22:11:04
 */
@RestController
@RequestMapping("result")
public class ResultController {

    @Resource
    private ResultService Service;

    @PostMapping("/save")
    @ResponseBody
    public Object save(@RequestBody Result result) {
        System.out.println("/result/save");

        result.setTime(new Timestamp(System.currentTimeMillis()));
        System.out.println(result.toString());
        Result resultInfo = Service.insert(result);

        BaseResp baseResp = new BaseResp();
        baseResp.setCode(BaseResp.LOGIN_SUCCESS);
        baseResp.setMsg("筛查结果已经保存");
        baseResp.setData(resultInfo);

        return baseResp;
    }

    @PostMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Result result, HttpServletRequest request) {
        System.out.println("/result/update");

        String token = request.getHeader("Authorization");
        System.out.println(token);
        String userid = JWTUtil.getToken(token).getClaim("id").asString();
        System.out.println("userid:" + userid);

        result.setTime(null);
        result.setUserid(Integer.parseInt(userid));
        System.out.println(result.toString());

        Service.update(result);

        BaseResp baseResp = new BaseResp();
        baseResp.setCode(BaseResp.LOGIN_SUCCESS);
        baseResp.setMsg("筛查结果已修改");

        return baseResp;
    }

    //获取诊断记录
    @GetMapping("/selectAll")
    public Object selectAll(HttpServletRequest request) {

        String token = request.getHeader("Authorization");
        System.out.println(token);
        String id = JWTUtil.getToken(token).getClaim("id").asString();

        System.out.println("selectAll: " + id);
        List<Result> list = Service.selectAll(Integer.parseInt(id));
        return list;
    }

    //删除诊断记录
    @GetMapping("/delete/{id}")
    public Object delete(@PathVariable("id") Integer id) {
        System.out.println("delete/{id}：  " + id);
        Service.deleteById(id);
        return "delete/{id}";
    }

    //更改权限
    @GetMapping("/changeStatus")
    public Object changeStatus(Integer id, Integer status) {
        System.out.println("changeStatus:  " + id + "  " + status);
        Result result = new Result();
        result.setId(id);
        result.setStatus(status);

        Service.update(result);

        return "changeStatus";
    }

}
